# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         test12
# Description:   例11.10
# Author:       ASUS
# Date:         2019/3/26
#-------------------------------------------------------------------------------


import re
#sub函数函数返回替换后的结果
result = re.sub("Bill","Mike","Bill is my son")
print(result)
#subn函数返回一个元组，第一个元素是返回后的结果，第二个元素是替换的总数
result = re.subn("Bill","Mike","Bill is my son,I like Bill")
print(result)
print(result[0])
print("替换总数","=",result[1])
#使用“\N”形式应用匹配字符串中的分组
result = re.subn("([0-9])([a-z]+)",r"产品编码(\1-\2)","01-1abc,02-2xyz,03-9hgf")
print(result)
#替换部分可以是个函数，该函数返回用于替换的字符串
def fun():
    return r"产品编码(\1-\2)"
result = re.subn("([0-9])([a-z]+)",fun(),"01-1abc,02-2xyz,03-9hgf")
print(result)
print(result[0])
print("替换总数","=",result[1])